package RunableClass.useClass;






import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.String;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author javadb.com
 */
public class ReadNews  {
String symbolx;int Block[]=new int[12];
public static String[]sym=new String[5];String x ="";
public static  List<String>[] lst = new List[200];  
                  public int countx=0;
    /**
     * Reads text from a file line by line
     */
    public List[] readFromFile(String filename,String SET) throws SQLException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya = null;
       
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new StringReader(filename));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                 int count3=0;int d[]=new int[1000];String URL[]=new String[1000];
                  int count1 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                    if(line.contains("<td align=\"center\" width=\"20%\"><b>")){
                       
                            for(int i=0;i<5;i++){
                                char xx=line.charAt(i);
                                
                                if(xx>='0'&&xx<='9'){
                                    linenumber+=line.substring(i, i+1);       
                                }        
                            }
                            
                                lst[countx]=new LinkedList<>();
                                      lst[countx].add(linenumber);
                                      
                            
                       if(line.startsWith((linenumber))){
                    
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count1;
                         b[count1]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }
                      
                    //  System.out.println(  b[count1]+"-" + c[count2]);
                        if(a=='?'){
                           
                          //d[count3]=i; 
                            String URLx=line;
                         int index1=0;int index2=0;
                           for(int U=i;U<URLx.length();U++){
                                index1=i;
                              if(URLx.charAt(U)=='\''){
                                  System.out.print(U);
                                  index2=U;
                                break;
                              }
                                
                           }
                          //  System.out.println();
                          URL[count3]="http://www.efinancethai.com/LastestNews/index.aspx"+URLx.substring(index1, index2); 
                  //      System.out.println(URL[count3]);
                   //   System.out.println( d[count3]+"->http://www.efinancethai.com"+line.substring(i-23, i+41));
                           ++count3; 
                         
                        }
                        
                       
                        }
                       
                       //  System.out.print("count1->"+ b[count1]+",");
                       //   System.out.print("count2->"+ b[count2]+",");
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                            // if(isDigit(line.substring(b[i]+1,c[i]))){
                              
                               
                                 ya=line.substring(b[i]+1,c[i]);
                              if(i==3){ 
                                  lst[countx].add(ya);
                               //System.out.print( ya+"#");
                              }
                                  if(i==4){
                                      lst[countx].add(line.substring(b[i]+19,c[i]));
                                // System.out.print( ya=+"#");
                                  }
                                   if(i==8){
                                       if(ya.equals("&nbsp;")){
                                            lst[countx].add("-");
                                       }else{
                                       lst[countx].add(ya);
                                       }
                                     // System.out.print( ya+"#");
                                  }
                                 if(i==13){
                                     lst[countx].add(ya);
                                     //  System.out.print( ya+"#");
                                    }
       
                        } }
                   
                   lst[countx].add(String.valueOf(URL[0]));  
                   //lst[countx].add(ya+">"+countx);
                    ++countx;

                  
                        }  
                       
                     
                    }
    
            }
           
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(ReadNews.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
            }
        }
        return lst;
    }

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) throws SQLException {
       
       
     //   DefaultTableModel model = new DefaultTableModel();
          //  STOCK.jTable1.setModel(model);
            try {
             ReadNews RN=  new ReadNews();
             RN.readFromFile("NewsFile.txt","");
               for(int l=0;l<lst.length;l++){
                      if(lst[l] != null){
                       if(l==RN.countx){
                            break;
                         } 
                       //  System.out.println(lst[l]);
          //                //System.out.println(lst[l].get(0).toString()+lst[l].get(1).toString()+lst[l].get(2).toString()+lst[l].get(3).toString()); 
                     // String[]  socrates = { "Socrates", "", "469-399 B.C." };
                         
                       //  model.addRow(socrates);
                     }
                 // System.out.println((char)70);" 
                  //System.out.print(sym[l]+"@");
           }
                   
        //      //  System.out.println("Download Complete");
         } catch (SQLException ex) {
               ex.printStackTrace();
          }
       
 
}

        /* new ReadNews().readFromFile("NewsFile.txt","");
     
          for(int l=0;l<lst.length;l++){
              if(!lst[l].equals(null)){
                  System.out.println(lst[l].get(0)); 
              }
          
          //System.out.print(sym[l]+"@");
      }
            
      //for(int l=0;l<sym.length;l++){
            
       //     System.out.print(sym[l]+"@");
     // }
      
    }
    */
     public static boolean isDigit(String s)   {
     boolean right = false;
      char c = 0;
      for (int index = 0; index < s.length(); index ++)  {
            c = s.charAt(index);
            if (c >= '0' || c <= '9'||c=='.')  {
            right = true;
           }
       }
      return right;
}
        
}

